package com.hengx.cellview;

import com.hengx.cellview.cell.Cell;

public class CellMap {
    
    public Cell[][] datas;
    private int width, height;
    
    public void setWidth(int width) {
        if (width < 0) throw new IndexOutOfBoundsException("宽度不能小于0");
        if (this.width < width) {
            Cell[][] newDatas = new Cell[width][height];
            for(int x = 0; x < this.width; x++) {
                for(int y = 0; y < this.height; y++) {
                    newDatas[x][y] = this.datas[x][y];
                }
            }
            this.datas = newDatas;
        }
    	this.width = width;
    }
    
    public void setHeight(int height) {
        if (height < 0) throw new IndexOutOfBoundsException("高度不能小于0");
        if (this.height < height) {
            Cell[][] newDatas = new Cell[width][height];
            for(int x = 0; x < this.width; x++) {
                for(int y = 0; y < this.height; y++) {
                    newDatas[x][y] = this.datas[x][y];
                }
            }
            this.datas = newDatas;
        }
    	this.height = height;
    }
    
    public Cell get(int x, int y) {
        if (x < 0 || x >= width || y < 0 || y >= height) {
            throw new IndexOutOfBoundsException("索引越界: x=" + x + ", y=" + y);
        }
    	return datas[x][y];
    }
    
    public void set(int x, int y, Cell value) {
        if (x < 0 || x >= width || y < 0 || y >= height) {
            throw new IndexOutOfBoundsException("索引越界: x=" + x + ", y=" + y);
        }
        datas[x][y] = value;
    }
    
    public int getWidth() {
    	return this.width;
    }
    
    public int getHeight() {
    	return this.height;
    }
    
    public boolean isEmpty() {
    	return this.width == 0 || this.height == 0;
    }
    
    public CellMap() {
        this(0, 0);
    }
    public CellMap(int width, int height) {
        this.width = width;
        this.height = height;
        this.datas = new Cell[width][height];
    }
    
}
